package com.hbzhit.modules.sys.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hbzhit.common.extension.BaseServiceImpl;
import com.hbzhit.common.utils.PageUtils;
import com.hbzhit.common.utils.Query;
import com.hbzhit.common.utils.R;
import com.hbzhit.modules.sys.entity.ProUserEntity;
import com.hbzhit.modules.sys.service.ProUserService;
import com.hbzhit.modules.sys.service.SysOrgService;
import com.hbzhit.modules.sys.vo.ProInfoVO;
import org.springframework.stereotype.Service;
import com.hbzhit.modules.sys.dao.ProDao;
import com.hbzhit.modules.sys.entity.ProEntity;
import com.hbzhit.modules.sys.service.ProService;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * @author qq
 * @date 2022-04-22 16:15:46
 **/
@Service("proService")
public class ProServiceImpl extends BaseServiceImpl<ProDao, ProEntity> implements ProService {

    @Resource
    private ProUserService proUserService;
    @Resource
    private SysOrgService orgService;

    @Override
    public PageUtils queryPage(Page page, Map<String, Object> params) {
        return new PageUtils(this.baseMapper.selectPageBy(page, this.queryWrapperHandle(params)));
    }

    @Override
    @Transactional
    public R<ProEntity> savePro(ProEntity proEntity) {
        Long userId = orgService.queryUserIdByOrgId(proEntity.getOrgId());
        if (ObjectUtil.isEmpty(userId)) return R.error("该部门无部门负责人，请选择其他部门！");
        boolean flag = this.save(proEntity);
        if (flag) {
            ProUserEntity newProUser = new ProUserEntity();
            newProUser.setProId(proEntity.getId());
            //添加项目经理
            newProUser.setUserId(Long.parseLong(proEntity.getProManager()));
            newProUser.setType(1);
            proUserService.save(newProUser);
            //添加部门经理
            newProUser.setUserId(userId);
            newProUser.setType(2);
            proUserService.save(newProUser);
        }
        return R.ok(proEntity);
    }

    @Override
    public ProInfoVO queryProInfo(Long proId) {
        return this.baseMapper.selectProInfo(proId);
    }

}

